package tt.dz.egobus.servicemanager.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.egobus.base.dao.BaseDao;
import tt.dz.egobus.servicemanager.base.service.AbstractExtendService;
import tt.dz.egobus.servicemanager.dao.BusServiceDao;
import tt.dz.egobus.servicemanager.entities.BusService;
import tt.dz.egobus.servicemanager.service.BusServiceService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Anson
 * @date 17-3-15
 * @name
 * @since 1.0.1
 */
@Service("busServiceServiceImpl")
public class BusServiceServiceImpl extends AbstractExtendService<BusService, String> implements BusServiceService {

    @Autowired
    @Qualifier("busServiceDaoImpl")
    private BusServiceDao dao;

    @Override
    protected BaseDao<BusService, String> getSelfDao() {
        return this.dao;
    }

    @Override
    @Transactional
    public boolean isExistBusinessCode(int businessCode, String execludeId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("eq#businessCode", businessCode);
        if (!StringUtils.isEmpty(execludeId)) {
            param.put("ne#id", execludeId);
        }
        return this.dao.count(param) > 0;
    }

    @Override
    @Transactional
    public boolean isBusinessCodeActive(int businessCode) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("eq#businessCode", businessCode);
        param.put("eq#isActive", true);
        return this.dao.count(param) > 0;
    }

    @Override
    @Transactional
    public BusService getBusServiceByBusinessCode(int businessCode) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("eq#businessCode", businessCode);
        param.put("eq#isActive", true);
        List<BusService> lists = this.dao.list(param);
        return lists.isEmpty() ? null : lists.get(0);
    }

    @Override
    @Transactional
    public List<BusService> getServices(Integer[] businessCodes) {
        String hqlQueryName = "queryServiceByCodes";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("businessCodes", businessCodes);
        return (List<BusService>) this.list(hqlQueryName, param, null);
    }
}
